package brainbots.bot;

import java.awt.Color;
import java.io.Serializable;
import java.util.Random;

/*describes coloring of the bot*/
public class BotColors implements Serializable{
	public static final int NUM_COLORS = 6;
	private Color[] colors = new Color[NUM_COLORS];
	/**Generate random coloring*/
	public static BotColors random(){
		BotColors clr = new BotColors();
		for (int i =0; i<clr.colors.length;++i){
			clr.setColor(i, randomColor());
		}
		return clr;
	}
	
	public static BotColors hybride(BotColors clr1, BotColors clr2){
		BotColors rval = new BotColors();
		for (int i =0; i<NUM_COLORS; ++i){
			Color c1 = clr1.get(i);
			Color c2 = clr2.get(i);
			Color c3 = null;
			//4 choises: take from parent1, take from parent2, merge, random
			double p = Math.random();
			if (p <=0.3){
				c3 = c1;
			}else if (p<=0.6){
				c3 = c2;
			}else if (p<=0.9){
				c3 = mergeColors(c1, c2);
			}else{
				c3 = randomColor();
			}
			rval.setColor(i, c3);
		}
		return rval;
	}
	private static Color mergeColors(Color c1, Color c2) {
		return new Color( 
				(c1.getRed()+c2.getRed())/2, 
				(c1.getGreen()+c2.getGreen())/2,
				(c1.getBlue()+c2.getBlue())/2  );
	}

	private void setColor(int idx, Color clr) {
		colors[idx]=clr;
	}
	private static Color randomColor() {		
		return new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
	}
	public final Color get(int idx){
		return colors[idx];
	}
	
}
